package com.maszy.commons.tools;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.math.BigDecimal;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import javax.print.Doc;
import javax.print.DocFlavor;
import javax.print.DocPrintJob;
import javax.print.PrintException;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.SimpleDoc;
import javax.print.attribute.DocAttributeSet;
import javax.print.attribute.HashDocAttributeSet;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.MediaSizeName;
import javax.swing.JOptionPane;


import com.maszy.bean.GoodPlan;
import com.maszy.bean.Product;
import com.maszy.service.GoodPlanService;



public class PrintTool implements Printable{
	   private int PAGES = 0;
	   /*
	   * Graphic指明打印的图形环境；PageFormat指明打印页格式（页面大小以点为计量单位，
	   * 1点为1英寸的1/72，1英寸为25.4毫米。A4纸大致为595×842点）；page指明页号
	   */
	   private final int _titleFontSize=15;
	   private final int _contentFontSize=12;
	   private final int _x=20;
	   private final int _x2=300;
	   private final int _y=20;
	   private final int step=20;
	   String goodType="";
	   private GoodPlan goodplan;
	   public PrintTool(GoodPlan good){
		   this.goodplan=good;
	   }
	   public GoodPlan getGoodplan() {
		return goodplan;
	}
	public void setGoodplan(GoodPlan goodplan) {
		this.goodplan = goodplan;
	}
	public int print(Graphics gp, PageFormat pf, int page)
	           throws PrinterException {		 
		   int y=0;
		   goodType="原材料供应单";
		 /* if("1".equals(goodplan.getGoodPlan_Type())){
			   goodType="原材料供应单";
		   }*/
		  
	       Graphics2D g2 = (Graphics2D) gp;
	       Graphics2D g3 = (Graphics2D) gp;
	       g2.setPaint(Color.black); // 设置打印颜色为黑色	     
	       if (page >= PAGES) // 当打印页号大于需要打印的总页数时，打印工作结束
	           return Printable.NO_SUCH_PAGE;
	       
	       
	       g2.translate(pf.getImageableX(), pf.getImageableY());// 转换坐标，确定打印边界	 	       
	       g2.setFont(new Font("宋体", Font.PLAIN, _titleFontSize));	   
	       g2.drawString(goodType+"----"+goodplan.getDept_Name()+"  ("+goodplan.getDoorStore_Name()+")", _x+150,_y); // 具体打印每一行文本，同时走纸移位
	       g2.setFont(new Font("宋体", Font.PLAIN,_contentFontSize));	
	       y=_y+step;
	       g2.drawString("申请人:"+goodplan.getGoodPlan_User().trim()+"  "+goodplan.getGoodPlan_date(),_x+170,y);
	       g2.drawString("-----------------------------------------------------------------------------------------------------",_x-25,y+10);
	       y=y+step;
	       g2.drawString(Commons.FillSpace("序号",5)+Commons.FillSpace("名称",12)+Commons.FillSpace("数量",8)+Commons.FillSpace("规格",11)+Commons.FillSpace("单位",4), _x, y);
	       if(goodplan.getProductList()!=null){
		       for(int i=0;i<goodplan.getProductList().size();i++){
		    	   String name=goodplan.getProductList().get(i).getProduct_Name().trim();
		    	   String quantity=goodplan.getProductList().get(i).getProduct_Quantity().trim();
		    	   String unit=goodplan.getProductList().get(i).getProduct_Unit().trim();
		    	   String guige=goodplan.getProductList().get(i).getProduct_Specification().trim();
		    	   if(i>35){
		    		   if(i==36){
		    			   y=_y+step+step;
			    	       g2.drawString(Commons.FillSpace("序号",5)+Commons.FillSpace("名称",12)+Commons.FillSpace("数量",8)+Commons.FillSpace("规格",11)+Commons.FillSpace("单位",4), _x2, y); 
		    		   }
		    	       y=y+step;
		    		   g2.drawString(Commons.FillSpace(Integer.toString(i+1),5)+Commons.FillSpace(name,12)+Commons.FillSpace(quantity,8)+Commons.FillSpace(guige,11)+Commons.FillSpace(unit,4), _x2, y);
		    	   }
		    	   else if(i<36){
		    		   y=y+step;
		    		   g2.drawString(Commons.FillSpace(Integer.toString(i+1),5)+Commons.FillSpace(name,12)+Commons.FillSpace(quantity,8)+Commons.FillSpace(guige,11)+Commons.FillSpace(unit,4), _x, y);
		    	   }
		    	  
		       }
	       }
	     
	       return Printable.PAGE_EXISTS; // 存在打印页时，继续打印工作
	   }
	   // 打印内容到指定位置
	   public void printContent() {	 
		   		String printName="print1";
	           PAGES = 1; // 获取打印总页数
	           // 指定打印输出格式
	           DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
	           DocAttributeSet das = new HashDocAttributeSet();
	           // 定位默认的打印服务
//	           PrintService printService = PrintServiceLookup.lookupDefaultPrintService();
	         /*  if("1".equals(goodplan.getGoodPlan_Type())){
				   printName="2lou";
			   }
			   if("2".equals(goodplan.getGoodPlan_Type())){
				   printName="4lou";
			   }*/
			   
			 //  printName=goodplan.getPrint_name();
			   PrintService[] printServices=PrintServiceLookup.lookupPrintServices(flavor, das);
			   for(int i=0;i<printServices.length;i++){
				// System.out.println(printServices[i].getName()+"已打印"+"  printName:"+printName);
				   if(printName.equals(printServices[i].getName())){
					   // 创建打印作业
			           DocPrintJob job = printServices[i].createPrintJob();
			           // 设置打印属性
			           PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
			           // 设置纸张大小,也可以新建MediaSize类来自定义大小
			           pras.add(MediaSizeName.ISO_A4);
			         
			           // 指定打印内容
			           Doc doc = new SimpleDoc(this, flavor, das);
			           // 不显示打印对话框，直接进行打印工作
			           try {
			               job.print(doc, pras); // 进行每一页的具体打印操作
			           } catch (PrintException pe) {
			               pe.printStackTrace();
			           }
			           

				   }
			   }
	        	    
	   }
	
	public static void main(String args[]) throws FileNotFoundException, MalformedURLException{
		GoodPlan obj=new GoodPlan();
		obj.setGoodPlan_Id(137L);
		GoodPlanService service=new GoodPlanService();
		GoodPlan goodPlan=service.PrintGoodPlan(obj);
		obj=service.changeGoodPlan(goodPlan);
		for(int j=0;j<1;j++){
			obj.getGoodPlanList().get(j).setGoodPlan_User(obj.getGoodPlan_User());
			obj.getGoodPlanList().get(j).setGoodPlan_date(obj.getGoodPlan_date());
			obj.getGoodPlanList().get(j).setDoorStore_Name(obj.getDoorStore_Name());
			obj.getGoodPlanList().get(j).setPrint_name(service.queryByDeptName(obj.getGoodPlanList().get(j).getDept_Name()).getPrint_name());
			PrintTool ptool=new PrintTool(obj.getGoodPlanList().get(j));
			ptool.printContent();
		}
	}
}
